package com.miyake.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.miyake.client.datamanage.NodeProperty;
import com.miyake.server.dsaccess.NodeManager;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "false")
public class DsNode {
	@PrimaryKey 
	@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)  
	private Key key;
	
	@Persistent
	private String parentKey;
	
	@Persistent
	private String name;
	
	@Persistent
	private Integer type;
	
	@Persistent
	private String substKey;

	@Persistent
	private String owner;
	
	@Persistent
	private Integer securityLevel;
	
	@Persistent
	private List<String> subNodes = new ArrayList<String>();
	
	public DsNode(String parentKey, String name, Integer type) {
		super();
		this.name = name;
		this.type = type;
		this.parentKey = parentKey;
	}

	public String getEncodedKey() {
		return KeyFactory.keyToString(key);
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public String getParentKey() {
		return parentKey;
	}

	public void setParentKey(String parentKey) {
		this.parentKey = parentKey;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public String getSubstKey() {
		return substKey;
	}

	public void setSubstKey(String substKey) {
		this.substKey = substKey;
	}
	
	public NodeProperty toRemoteObject() {
		List<DsNode> list = this.getSubNodesObject();

		NodeProperty ret = new NodeProperty(name, getEncodedKey(), type);
		ret.setSecurityLevel(securityLevel);
		ret.setParentKey(this.parentKey);
		for (DsNode node : list) {
			ret.addChild(node.toRemoteObject());
		}
		return ret;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public void setSubNodes(List<String> subNodes) {
		this.subNodes = subNodes;
	}

	public DsNode getParent() {
		return NodeManager.getInstance().getNode(parentKey);
	}
	
	public List<DsNode> getSubNodesObject() {
		List<DsNode> ret = new ArrayList<DsNode>();
		for (String s : subNodes) {
			DsNode node = NodeManager.getInstance().getNode(s);
			ret.add(node);
		}
		
		return ret;
//		return NodeManager.getInstance().getSubNodes(getEncodedKey());
	}

	public List<String> getSubNodes() {
		return this.subNodes;
	}
	
	public void addSubNode(String encodedKey, Integer order) {
		// order handling should be implemented later
		this.subNodes.add(encodedKey);
	}

	public Integer getSecurityLevel() {
		return securityLevel;
	}

	public void setSecurityLevel(Integer securityLevel) {
		this.securityLevel = securityLevel;
	}

	public void upSubNode(String key2) {
		int i = 0;
		for (String s : subNodes) {
			if (s.equals(key2)) {
				subNodes.remove(s);
				subNodes.add(i - 1, s);
				break;
			}
			i++;
		}
	}
	public void downSubNode(String key2) {
		int i = 0;
		for (String s : subNodes) {
			if (s.equals(key2)) {
				subNodes.remove(s);
				subNodes.add(i + 1, s);
				break;
			}
			i++;
		}
	}
	
}
